package com.gxa.shop.servlet.address;

import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.dto.AccountDto;
import com.gxa.shop.bean.dto.AddressDto;
import com.gxa.shop.bean.pojo.Brand;
import com.gxa.shop.bean.vo.AddressVo;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.dao.address.AddressDao;
import com.gxa.shop.dao.address.impl.AddressDaoImpl;
import com.gxa.shop.util.MyFormatUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

@WebServlet(name = "SelectAddressServlet", value = "/address/select")
public class SelectAddressServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 直接获得全部收获地址
        Integer page= MyFormatUtils.toInteger(req.getParameter("page"));
        Integer limit= MyFormatUtils.toInteger(req.getParameter("limit"));
        String name=MyFormatUtils.trim(req.getParameter("addressName"));
        Integer id=MyFormatUtils.toInteger(req.getParameter("expressfeeId"));

        AddressDto addressDto=new AddressDto();
        addressDto.setPage(page);
        addressDto.setLimit(limit);
        addressDto.setAddressName(name);
        addressDto.setExpressfeeId(id);
        AddressDao addressDao=new AddressDaoImpl();
        Long total=addressDao.totalAllAddress(addressDto);
        R r;
        List<AddressVo>  list = Collections.emptyList();
        if (total>0){
            list=addressDao.selectAllAddress(addressDto);
        }
        r=R.ok().addData("total",total).addData("list",list);
        String listResult=JSON.toJSONString(r);
        System.out.println(listResult);
        PrintWriter writer=resp.getWriter();
        writer.write(listResult);
        writer.flush();
        writer.close();

    }
}
